package _18_剑指OfferII;

import _18_剑指OfferII.datastrcute.TreeNode;

public class _051_剑指OfferII节点之和最大的路径 {

    int max = Integer.MIN_VALUE;

    public int maxPathSum(TreeNode root) {
        dfs(root);
        return max;
    }

    private int dfs(TreeNode root) {
        if (root == null) return 0;

        int left = dfs(root.left);

        int right = dfs(root.right);

        max = Math.max(max, root.val + left + right);

        return Math.max(Math.max(left, right) + root.val, 0);
    }

}
